/**
 * @author gjt
 */
let str= 'http://localhost:8080/fileUpload/timg.jpg';
window.onload=function(){
	$("#img0").attr("src",str);
	show();
}

function getObjectURL(file){
	var url = null;
	if(window.createObjectURL !=undefined){
		url = window.createObject(file);
	}else if(window.URL !=undefined){
		url = window.URL.createObjectURL(file);
	}else if(window.webkitURL !=undefined){
		url = window.webkitURL.createObjectURL(file);
	}
	return url;
}
$(function(){	
	$("#file0").change(function(){
		var objUrl=getObjectURL(this.files[0]);
		console.log("obUrl = " + objUrl);
		if(objUrl){
			$("#img0").attr("src",objUrl);
		}
	});
})

function getPicture(){ 
	var formData = new FormData($( "#form" )[0]);
	$.ajax({  
		url: '../FileUpload',  
		type: 'POST',  
		data: formData,  
		cache: false,
		async:false,
		contentType: false,  
		processData: false,  //序列化
		success: function (data) {  
			$("#img0").attr("src",str);
			show(); 
		},  
		error: function (data) {  
			alert("上传失败");  
		}  
	});   
}	

function upLoadImage(){
	getPicture();
}

/**
 * 读取图片
 * @returns
 */
function show(){
	$.ajax({  
		url: '../test2',  
		type: 'GET',  
		data: {
			method:'show',
			inputData: '1'
		},  
		cache: false,
		async:false,
		dataType: "json",  // 前后台数据交换类型
		success: function (data) {  
			setImage(data);
		},  
		error: function (data) {  
			alert("上传失败");  
		}  
	});   
}

/**
 * 展示图片
 * @param data 传输回来json数据
 * @returns
 */
function setImage(data){
	console.log(data.imageUrl);
	$(".show").attr("src",data.imageUrl);
}